import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class Demo01 {
    /*
    1.在当前目录路径中创建文件demo01.txt
    2.统计文件中e和a的出现次数
    3.文件内容：hello my name is yunding.
     */
    String filePath = "c:\\Users\\86195\\IdeaProjects\\IODemo\\demo01.txt";
    public void writer() {
        FileWriter fileWriter = null;
        try {
            fileWriter = new FileWriter(filePath);
            fileWriter.write("hello my name is yunding");
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                fileWriter.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
    public void count(){
        FileReader fileReader = null;
        int data = 0;
        int countE = 0;
        int countA = 0;
        try {
            fileReader = new FileReader(filePath);
            while ((data=fileReader.read()) != -1){
                if(((char)data)=='e'){
                    countE ++;
                }
                else if(((char)data)=='a'){
                    countA ++;
                }
            }
            System.out.println("e出现的次数:"+countE+'\n');
            System.out.println("a出现的次数:"+countA);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                if(fileReader != null){
                    fileReader.close();
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
}